package role

import (
	"context"
	"time"

	"go-weixin/app/usercenter/api/internal/svc"
	"go-weixin/app/usercenter/api/internal/types"
	"go-weixin/app/usercenter/rpc/pb/proto/usercenter"

	"github.com/zeromicro/go-zero/core/logx"
)

type RoleGetPowerLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRoleGetPowerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RoleGetPowerLogic {
	return &RoleGetPowerLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RoleGetPowerLogic) RoleGetPower(req *types.RoleGetPowerReq) (resp *types.RoleGetPowerResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.UserRpc.RoleGetPower(ctx, &usercenter.RoleGetPowerReq{
		Id: req.RoleId,
	})
	var powerList []types.Power
	if len(res.PowerList) > 0 {
		for _, power := range res.PowerList {
			powerList = append(powerList, types.Power{
				Id:       power.Id,
				Name:     power.Name,
				Url:      power.Url,
				ParentId: power.ParentId,
				Method:   power.Method,
				CreateAt: power.CreateAt,
				UpdateAt: power.UpdateAt,
			})
		}
	}
	resp = new(types.RoleGetPowerResp)
	resp.PowerList = powerList
	return
}
